package com.cgly.medical.controller;

import com.alibaba.fastjson.JSON;
import com.cgly.medical.config.VerificationCode;
import com.cgly.medical.entity.AccountModel;
import com.cgly.medical.util.JwtUtils;
import com.cgly.medical.util.Msg;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

/**
 * @program: CGLY
 * @description:
 * @author: chen di
 * @create: 2022-08-25 10:39
 */
@Api(description = "登录模块")
@RestController
public class LoginController {
    @PreAuthorize("hasRole('ROLE_1')")
    @GetMapping("/mobile")
    public String mobilequery()
    {
        System.out.println("访问mobile");
        return "访问mobile";
    }

    @GetMapping("/salary")
    public String salaryquery() {

        System.out.println("访问salary");
        return "访问salary";
    }
    @GetMapping("/test")
    public String test() {

        System.out.println("访问test");
        return "访问test";
    }

    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        VerificationCode code = new VerificationCode();
        BufferedImage image = code.getImage();
        String text = code.getText();
        HttpSession session = request.getSession(true);
        session.setAttribute("verify_code", text);
        VerificationCode.output(image,resp.getOutputStream());
    }

    @PostMapping("/user/logout")
    public Msg logout(){
        return Msg.success().mess("退出成功");
    }

    @GetMapping("/login/getUser/info/{token}")
    public Msg getInfo(@PathVariable String token){
        Map<String,Object> map= JwtUtils.parseToken((String) JSON.parse(token));
//        String acct=(String)map.get("acct");
        String name=(String)map.get("name");
//        System.out.println(acct);
        return Msg.success().mess("获取信息成功").data("name",name);
    }
}